﻿#include "lix/lix.h"
#include "lix/char_sequence.h"

#include "lix/xml/Node.h"

namespace lix{	
	namespace xml{

//==============================================================================
// lix::xml::Node
//==============================================================================

Node::Node( NodeType type )
{
	m_Type = type;
	m_Children = m_Attributes = m_Next = NULL;
}

Node::Node( const Node& node )
{
	this->m_Type = node.m_Type;
	this->m_Name		= node.m_Name;
	this->m_Value		= node.m_Value;
	this->m_Children	= node.m_Children;
	this->m_Attributes	= node.m_Attributes;
	this->m_Next		= node.m_Next;
}

Node::~Node()
{

}


// 子供の追加
void Node::addChild( Node* child )
{
	LIX_ASSERT( m_Type != kNodeRoot || m_Type != kNodeElement ); 

	// 子供がいなければ最初の子供として
	if( m_Children == NULL ){
		m_Children = child;
		return;
	}
	
	getLastChild()->m_Next = child;
}

void Node::addAttribute( Node* attribute )
{
	// 子供がいなければ最初の子供として
	if( m_Attributes == NULL ){
		m_Attributes = attribute;
		return;
	}
	
	getLastAttribute()->m_Next = attribute;
}



Node* Node::getLastItem( Node* node )
{
	if( node == NULL ){
		return NULL;
	}

	while( true ){
		if( node->m_Next == NULL ){
			return node;
		}
		node = node->m_Next;
	}
}

const Node* Node::findFirstElementByName( const char* name )const 
{
	const Node* n = getFirstChild();
	while( n ){
		if( n->getType() == kNodeElement ){
			if( n->name() == name ){
				return n;
			}
		}
		n = n->getNextSibling();
	}
	return NULL;
}

lix::CharSequence Node::getFirstChildText() const
{
	const Node* child = getFirstChild();
	if( ! child ){
		return CharSequence();
	}
	return child->value();
}









	} // namespace xml
} // namespace lix


